<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */

use App\Server\TcpServer;
use App\Server\UdpServer;
use Hyperf\Server\Event;
use Hyperf\Server\Server;
use Swoole\Constant;

return [
    'mode' => SWOOLE_PROCESS,
    'servers' => [
        [
            'name' => 'http',
            'type' => Server::SERVER_HTTP,
            'host' => '0.0.0.0',
            'port' => 9501,
            'sock_type' => SWOOLE_SOCK_TCP,
            'callbacks' => [
                Event::ON_REQUEST => [Hyperf\HttpServer\Server::class, 'onRequest'],
            ],
            'options' => [
                // Whether to enable request lifecycle event
                'enable_request_lifecycle' => false,
            ],
            'settings' => [
                //'open_websocket_protocol' => false,
                // 静态资源
                'document_root' => BASE_PATH . '/public',
                'enable_static_handler' => true,
            ],
        ],
        [
            'name' => 'innerHttp',
            'type' => Server::SERVER_HTTP,
            'host' => '0.0.0.0',
            'port' => 9502,
            'sock_type' => SWOOLE_SOCK_TCP,
            'callbacks' => [
                Event::ON_REQUEST => ['InnerHttp', 'onRequest'],
            ],
        ],
        //TCP Server (适配 jsonrpc 协议)
//        [
//            'name' => 'jsonrpc',
//            'type' => Server::SERVER_BASE,
//            'host' => '0.0.0.0',
//            'port' => 9503,
//            'sock_type' => SWOOLE_SOCK_TCP,
//            'callbacks' => [
//                Event::ON_RECEIVE => [\Hyperf\JsonRpc\TcpServer::class, 'onReceive'],
//            ],
//            'settings' => [
//                'open_eof_split' => true, // 启用 EOF 自动分包
//                'package_eof' => "\r\n", // 设置 EOF 字符串
//                'package_max_length' => 1024 * 1024 * 2,
//            ],
//        ],
//        [
//            'name' => 'socket-io',
//            'type' => Server::SERVER_WEBSOCKET,
//            'host' => '0.0.0.0',
//            'port' => 9509,
//            'sock_type' => SWOOLE_SOCK_TCP,
//            'callbacks' => [
//                Event::ON_HAND_SHAKE => [Hyperf\WebSocketServer\Server::class, 'onHandShake'],
//                Event::ON_MESSAGE => [Hyperf\WebSocketServer\Server::class, 'onMessage'],
//                Event::ON_CLOSE => [Hyperf\WebSocketServer\Server::class, 'onClose'],
//            ],
//        ],
        //jsonrpc-http 协议
        [
            'name' => 'jsonrpc-http',
            'type' => Server::SERVER_HTTP,
            'host' => '0.0.0.0',
            'port' => 9504,
            'sock_type' => SWOOLE_SOCK_TCP,
            'callbacks' => [
                Event::ON_REQUEST => [\Hyperf\JsonRpc\HttpServer::class, 'onRequest'],
            ],
        ],
        [
            'name' => 'tcp',
            'type' => Server::SERVER_BASE,
            'host' => '0.0.0.0',
            'port' => 9514,
            'sock_type' => SWOOLE_SOCK_TCP,
            'callbacks' => [
                Event::ON_RECEIVE => [TcpServer::class, 'onReceive'],
            ],
            'settings' => [
                // 按需配置
            ],
        ],
//        [
//            'name' => 'udp',
//            'type' => Server::SERVER_BASE,
//            'host' => '0.0.0.0',
//            'port' => 9515,
//            'sock_type' => SWOOLE_SOCK_UDP,
//            'callbacks' => [
//                Event::ON_PACKET => [UdpServer::class, 'onPacket'],
//            ],
//            'settings' => [
//                // 按需配置
//            ],
//        ],
        [
            'name' => 'ws',
            'type' => Server::SERVER_WEBSOCKET,
            'host' => '0.0.0.0',
            'port' => 9512,
            'sock_type' => SWOOLE_SOCK_TCP,
            'callbacks' => [
                Event::ON_HAND_SHAKE => [Hyperf\WebSocketServer\Server::class, 'onHandShake'],
                Event::ON_MESSAGE => [Hyperf\WebSocketServer\Server::class, 'onMessage'],
                Event::ON_CLOSE => [Hyperf\WebSocketServer\Server::class, 'onClose'],
            ],
        ],
    ],
    'settings' => [
        Constant::OPTION_ENABLE_COROUTINE => true,// 开启内置协程
        Constant::OPTION_WORKER_NUM => swoole_cpu_num(),// 设置启动的 Worker 进程数
        Constant::OPTION_PID_FILE => BASE_PATH . '/runtime/hyperf.pid', // master 进程的 PID
        Constant::OPTION_OPEN_TCP_NODELAY => true,// TCP 连接发送数据时会关闭 Nagle 合并算法，立即发往客户端连接
        Constant::OPTION_MAX_COROUTINE => 100000,// 设置当前工作进程最大协程数量
        Constant::OPTION_OPEN_HTTP2_PROTOCOL => true, // 启用 HTTP2 协议解析
        Constant::OPTION_MAX_REQUEST => 100000, // 设置 worker 进程的最大任务数
        Constant::OPTION_SOCKET_BUFFER_SIZE => 2 * 1024 * 1024,// 配置客户端连接的缓存区长度
        Constant::OPTION_BUFFER_OUTPUT_SIZE => 2 * 1024 * 1024,
        Constant::OPTION_DAEMONIZE => false,//设置守护进程化,程序将转入后台作为守护进程运行
//        Constant::OPTION_TASK_WORKER_NUM=>8,// Task Worker 数量，根据您的服务器配置而配置适当的数量
//        Constant::OPTION_TASK_ENABLE_COROUTINE => true,// 因为 `Task` 主要处理无法协程化的方法，所以这里推荐设为 `false`，避免协程下出现数据混淆的情况
        // 静态资源
        Constant::OPTION_DOCUMENT_ROOT=> BASE_PATH . '/public',
        Constant::OPTION_ENABLE_STATIC_HANDLER => true,
        //websocket配置
        Constant::OPTION_OPEN_WEBSOCKET_PONG_FRAME=>true,

        // Task Worker 数量，根据您的服务器配置而配置适当的数量
        'task_worker_num' => 1,
        // 因为 `Task` 主要处理无法协程化的方法，所以这里推荐设为 `false`，避免协程下出现数据混淆的情况
        'task_enable_coroutine' => false,

    ],
    'callbacks' => [
        Event::ON_WORKER_START => [Hyperf\Framework\Bootstrap\WorkerStartCallback::class, 'onWorkerStart'],
        Event::ON_PIPE_MESSAGE => [Hyperf\Framework\Bootstrap\PipeMessageCallback::class, 'onPipeMessage'],
        Event::ON_WORKER_EXIT => [Hyperf\Framework\Bootstrap\WorkerExitCallback::class, 'onWorkerExit'],
        Event::ON_TASK => [Hyperf\Framework\Bootstrap\TaskCallback::class, 'onTask'],
        Event::ON_FINISH => [Hyperf\Framework\Bootstrap\FinishCallback::class, 'onFinish'],
        ],
];
